<?php
namespace app\home\model;

use think\Model;
use think\Db;
use think\Request;
use think\Cache;
use think\Session;

class Users extends Model
{

    /**
     * 会员注册
     * @param $user_name 会员名称 ,$email  邮箱,$sex  性别 $rand_str  随机字符
     * @return array or null;
     */
    public function add_user_info(){


        $param = Request::instance()->post();

        $data = array();

        $mobile  = $param['mobile'];
        $pass_word = $param['user_pwd'];
        $sms_code = $param['verify_code'];



        $user = Db::table("users")->where("mobile",$mobile)->find();

        if($user != null){

            //账号已存在
            return 1;
            exit;

        }

        //验证 验证码
        if ($sms_code != Cache::get('mobile_code')) {

            return 2;
            exit;

        }

       $str = _rand_str(4);
      $data = [
          'mobile' => $mobile,
          'user_no' => $str,
          'user_pwd' => md5(md5($pass_word).$str),
          'user_time' => time(),
          'user_state' => 0,
          'user_grade' => 0,
          'user_integral'=>100
      ];


        $res = Db::table("users")->insert($data);
        if($res == 1){

            $res = Db::table("users")->where("mobile",$mobile)->find();

            Session::set('USERS',$res);

            $data1=[
                'score'=>100,
                'score_type'=>1,
                'data_remarks'=>'注册成功赠送100积分',
                'data_src'=>2,
                'score_time'=>time(),
                'user_id'=>Session::get('USERS.user_id')
            ];

             Db::table('score')->insert($data1);
            return 0;
        }else{
            return 3;
        }
    }
/*
 * 忘记密码
 *
 * */
    public function for_get_pwd($user_pwd,$mobile,$rand_str){

        $password= md5(md5($user_pwd).$rand_str);

//        $model= new Users();
//        $model->user_pwd=$password;
//        $model->mobile=$mobile;
        $res=Db::table('users')->where("mobile" , "$mobile")->update(['user_pwd'=>"$password",'user_no'=>$rand_str]);
//          echo $this->getLastSql();
//          exit;
        if ($res==false){
            return false;
        }else{
            return true;
        }
    }
}
